<template>
  <div class="xtx-form">
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-user"></i>
        <input v-model="form.account" class="input" type="text" placeholder="请输入用户名" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input v-model="form.mobile" class="input" type="text" placeholder="请输入手机号" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input v-model="form.code" class="input" type="text" placeholder="请输入验证码" />
        <span @click="sendCode" class="code">{{countTime===0 ? '发送验证码' :`${countTime}秒后尝试` }}</span>
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input v-model="form.password" class="input" type="password" placeholder="请输入密码" />
      </div>
      <div class="error"></div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-lock"></i>
        <input v-model="form.rePassword" class="input" type="password" placeholder="请确认密码" />
      </div>
      <div class="error"></div>
    </div>
    <a href="javascript:;" @click="submit" class="submit">立即提交</a>
  </div>
</template>

<script>
import { reactive } from '@vue/reactivity'
import { message } from '@/components/Message'
import { reqUserQQPatchCode, reqUserQQPatchLogin } from '@/api/user'
import { useCountDown } from '@/compositions'
export default {
  name: 'CallbackPatch',
  props: {
    unionId: {
      type: String,
      default: ''
    }
  },
  setup (props) {
    const form = reactive({
      mobile: '',
      code: '',
      account: '',
      password: '',
      rePassword: ''
    })
    const { countTime, start } = useCountDown(60)
    // 发送验证码请求
    const sendCode = async () => {
      if (!/^1[3-9]\d{9}$/.test(form.mobile)) return message({ text: '手机格式错误' })
      const { result } = await reqUserQQPatchCode(form.mobile)
      console.log(result)
      start()
    }
    // 发送登录请求
    const submit = async () => {
      // 校验密码输入一致
      if (form.password !== form.rePassword) return message({ text: '密码输入不一致' })
      const { result } = await reqUserQQPatchLogin({
        unionId: props.unionId,
        ...form
      })
      console.log(result)
    }
    return { form, sendCode, countTime, submit }
  }
}
</script>

<style scoped lang='less'>
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
